#include "LevelScene.h"
#include "MainLayer.h"
#include "GameScene.h"
#include "DBManager.h"
#include "DB_Data.h"
#include "LoadingScene.h"


CCScene* LevelScene::scene()
{
	CCScene * scene = NULL;
	do 
	{
		scene = CCScene::create();
		CC_BREAK_IF(! scene);

		LevelSelecterLayer *layer = LevelSelecterLayer::create();
		scene->addChild(layer);
	
		
	} while (0);

	// return the scene
	return scene;
}

bool LevelSelecterLayer::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(! CCLayer::init());

		CCSpriteBatchNode* batchNode =CCSpriteBatchNode::create("ui_atlas.png");

		CCSize size =CCDirector::sharedDirector()->getWinSize();

		CCMenuItemSprite *pbackItem = CCMenuItemSprite::create(
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(0, 92 ,50, 40)),
			CCSprite::createWithTexture(batchNode->getTexture(),CCRect(430 ,47, 50 ,40)),
			this,
			menu_selector(LevelSelecterLayer::menuBackCallback));
		//pbackItem->setAnchorPoint(ccp(pbackItem->getContentSize().width,pbackItem->getContentSize().height));
		pbackItem->setPosition(ccp(pbackItem->getContentSize().width, size.height -pbackItem->getContentSize().height));

		CCMenu* pMenu = CCMenu::create(pbackItem, NULL);
		pMenu->setPosition(CCPointZero);
		this->addChild(pMenu, 1);

		CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
		CC_BREAK_IF(! pLabel);

		pLabel->setPosition(ccp(size.width / 2, size.height - 50));

		this->addChild(pLabel, 1);
		CCRect level_icon_0 =CCRectMake(178, 0, 43, 45);
		CCRect level_icon_null =CCRectMake(90, 0, 43, 45);


		CCMenu* levelMenu = CCMenu::create();
		levelMenu->setPosition(CCPointZero);

		CCSprite* pSpriteitem =CCSprite::createWithTexture(batchNode->getTexture(),level_icon_null);
		int sWidth =pSpriteitem->getContentSize().width;
		int sHeight =pSpriteitem->getContentSize().height;
		int count;
		CCArray* carray =DBManager::sharedDBManager()->GetAbleLevelArray(1,&count);
		//	for (int i = 0; i < 10; i++)
		{
			CCObject* pObj = NULL;
			int i =0;
			int allcount =	(int)((size.width -sWidth ) /(sWidth +10));

			CCARRAY_FOREACH(carray,pObj)
			{
				float x =i %allcount * sWidth + sWidth + (i%allcount)*allcount *2;
				float y =size.height - ((i /allcount +2) * sHeight);

				LevelData *child = (LevelData*)pObj;
				CCSprite* pSprite_0=CCSprite::createWithTexture(batchNode->getTexture(),level_icon_0);
				CCMenuItemSprite *pSpriteItem = CCMenuItemSprite::create(
					pSprite_0,
					pSprite_0,
					this,
					menu_selector(LevelSelecterLayer::levelSelectedCallback));

				pSpriteItem->setPosition(ccp(x ,y));
				levelMenu->addChild(pSpriteItem,0,i);

				i++;
			}
			for (; i< count; i++)
			{
				CCSprite* pSprite_null =CCSprite::createWithTexture(batchNode->getTexture(),level_icon_null);
				float x =i %allcount * pSprite_null->getContentSize().width + pSprite_null->getContentSize().width + (i%allcount)*allcount *2;
				float y =size.height - ((i /allcount +2) * pSprite_null->getContentSize().height);
				CCMenuItemSprite *pSpriteItem = CCMenuItemSprite::create(
					pSprite_null,
					pSprite_null,
					this,
					menu_selector(LevelSelecterLayer::levelSelectedCallback));

				pSpriteItem->setPosition(ccp(x ,y));
				levelMenu->addChild(pSpriteItem,0,i);
			}

			/*	if(i >6)
			{
			level_icon_0 = level_icon_null;
			}
			CCSprite* pSprite0 =CCSprite::createWithTexture(batchNode->getTexture(),level_icon_0);
			int count =	(int)((size.width - pSprite0->getContentSize().width * 3) /(pSprite0->getContentSize().width +10));
			float x =i %count * pSprite0->getContentSize().width + pSprite0->getContentSize().width + (i%count)*count *2;

			float y =size.height - ((i /count +2) * pSprite0->getContentSize().height);
			*/
			//	pSprite0->setPosition(ccp(x ,y));


			//pMenu->addChild(pSpriteItem);
		}

		this->addChild(levelMenu, 0);

		CCSprite* pSprite =CCSprite::createWithTexture(batchNode->getTexture(),CCRect(45, 0 ,44, 46));// CCSprite::create("LRS_DUCK_SUCCESS.png");
		CC_BREAK_IF(! pSprite);

		pSprite->setPosition(ccp(size.width -pSprite->getContentSize().width , size.height - pSprite->getContentSize().height));

		this->addChild(pSprite, 0);

		bRet = true;
	} while (0);

	return bRet;
}

void LevelSelecterLayer::menuBackCallback(CCObject* pSender)
{

	CCDirector::sharedDirector()->replaceScene(MainLayer::scene());
}
void LevelSelecterLayer::levelSelectedCallback(CCObject* pSender)
{
	CCMenuItemSprite *pSpriteItem =(CCMenuItemSprite*)pSender;
	int tag =pSpriteItem->getTag();

	//	CCArray *obj =	pSpriteItem->getChildren();

	//CCScene *gameScene =GameLayer::scene(tag);
	LoadingScene *scene =LoadingScene::create();
	scene->sceneWithTargetSource(TargetSceneGame,tag);
	
	CCDirector::sharedDirector()->replaceScene(scene);
}
